#!/bin/bash

# --------------------------
# 导入数据中台数据
# --------------------------

mysql_file() {
  echo "执行MySQL文件：$1"
  /data/package/mysql/mysql -hmysql.ahcloud-private.com -P3307 -umysql -p$MYSQL_PASSWD < $1
}

mysql_sql() {
  echo "执行MySQL脚本：$1"
  /data/package/mysql/mysql -h mysql.ahcloud-private.com -P 3307 -umysql -p$MYSQL_PASSWD -e "$1"
}

clickhouse_sql() {
  echo "执行Clickhouse脚本：$1"
  curl -X POST -d "$1" 'http://ch1.ahcloud-private.com:8123/' -u default:${CLICKHOUSE_PASSWD}
}

mysql_sql "drop database if exists data_platform;"
mysql_sql "create database data_platform;"

mysql_file "mysql-ddl.sql"
mysql_file "mysql-dml.sql"

mysql_file "mysql-init.sql"
mysql_file "mysql-init-$DEPLOY_MODE.sql"

clickhouse_sql "drop database common_db;"
clickhouse_sql "drop database default;"

curl -X POST -d "drop database common_db on cluster cnwaf_1shard_cluster;" 'http://ch1.ahcloud-private.com:8123/' -u default:1qaz#EDC5tgb
curl -X POST -d "drop database default on cluster cnwaf_1shard_cluster;" 'http://ch1.ahcloud-private.com:8123/' -u default:1qaz#EDC5tgb

curl -X POST -d "drop table dwd_detail_mss_ailpha_bussasset__local on cluster cnwaf_1shard_cluster;" 'http://ch1.ahcloud-private.com:8123/' -u default:1qaz#EDC5tgb
curl -X POST -d "drop table dwd_detail_mss_ailpha_bussasset  on cluster cnwaf_1shard_cluster;" 'http://ch1.ahcloud-private.com:8123/' -u default:1qaz#EDC5tgb
curl -X POST -d "drop dictionary common_db.mss_1648232585246732289  on cluster cnwaf_1shard_cluster;" 'http://ch1.ahcloud-private.com:8123/' -u default:1qaz#EDC5tgb

curl -X POST -d "update analysis_quota_table set running_status='STOP', publish_status='UN_PUBLISHED' where deleted = 0;" 'http://ch1.ahcloud-private.com:8123/' -u default:${CLICKHOUSE_PASSWD}


/data/package/mysql/mysql -h mysql.ahcloud-private.com -P 3307 -umysql -p$MYSQL_PASSWD -e "update data_platform.analysis_quota_table set running_status='STOP', publish_status='UN_PUBLISHED' where deleted = 0 and running_status = 'FAIL';"